<?php defined('SYSPATH') or die('No direct script access.');

	class Jqgrid_Criteria {
		
		public $order_by = array();
		public $where = array(); //array de conditions
		
		public $page;
		public $limit;
		public $sidx;
		public $sord;
		
		public $isSearch = false;
		public $searchField;
		public $searchOper;
		public $searchString;
		
		public function calcOffset(){
			$page = ($this->page == 0)? $this->page : $this->page - 1;
			return $page * $this->limit; //calcula el desvío en base a la pagina y el numero de items por pagina a mostrar
		}
		
		public function getSearchWhere(){
			return $this->searchOperToSQL($this->searchField, $this->searchOper, $this->searchString);
		}
		
		public function searchOperToSQL($field, $oper,$val){
			if ($oper == "bw") return $field." LIKE '".$val."%'";
			if ($oper == "eq") return $field." = ".$val;
			if ($oper == "ne") return $field." <> ".$val;
			if ($oper == "lt") return $field." < ".$val;
			if ($oper == "le") return $field." <= ".$val;
			if ($oper == "gt") return $field." > ".$val;
			if ($oper == "ge") return $field." >= ".$val;
			if ($oper == "ew") return $field." LIKE '%".$val."'";
			if ($oper == "cn") return $field." LIKE '%".$val."%'";
			return false;
				
		}
		
		public function getSQLField($tablename){
			return $tablename.".".$this->searchField;
		}
		
		public function getSQLOper(){
			if (($this->searchOper == "bw") || ($this->searchOper == "ew") || ($this->searchOper == "cn")) return " LIKE ";
			if ($this->searchOper == "eq") return " = ";
			if ($this->searchOper == "ne") return " <> ";
			if ($this->searchOper == "lt") return " < ";
			if ($this->searchOper == "le") return " <= ";
			if ($this->searchOper == "gt") return " > ";
			if ($this->searchOper == "ge") return " >= ";
			return false;
		}
		
		public function getSQLSearchString(){
			if ($this->searchOper == "bw") return "".$this->searchString."%";
			if ($this->searchOper == "ew") return "%".$this->searchString;
			if ($this->searchOper == "cn") return "%".$this->searchString."%";
			return $this->searchString; 
		}
			
	}
?>